package com.linln.proxy.thread;

import com.github.monkeywie.proxyee.intercept.HttpProxyIntercept;
import com.github.monkeywie.proxyee.intercept.HttpProxyInterceptInitializer;
import com.github.monkeywie.proxyee.intercept.HttpProxyInterceptPipeline;
import com.github.monkeywie.proxyee.intercept.common.CertDownIntercept;
import com.github.monkeywie.proxyee.server.HttpProxyServer;
import com.github.monkeywie.proxyee.server.HttpProxyServerConfig;
import com.github.monkeywie.proxyee.util.HttpUtil;
import com.linln.proxy.utils.DateUtils;
import com.linln.proxy.utils.HttpClientInvok;
import com.linln.proxy.vo.HttpVo;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.*;

import java.net.URLEncoder;
import java.nio.charset.Charset;

public class ProxyThread implements Runnable{

    HttpClientInvok httpClientInvok;

    public ProxyThread(HttpClientInvok httpClientInvok){
        this.httpClientInvok = httpClientInvok;
    }
    @Override
    public void run() {
        HttpProxyServerConfig config = new HttpProxyServerConfig();
        config.setHandleSsl(true);
        new HttpProxyServer()
                .serverConfig(config)
                .proxyInterceptInitializer(new HttpProxyInterceptInitializer() {
                    @Override
                    public void init(HttpProxyInterceptPipeline pipeline) {
                        pipeline.addLast(new CertDownIntercept());
                        pipeline.addLast(new HttpProxyIntercept() {
                            @Override
                            public void beforeRequest(Channel clientChannel, HttpRequest httpRequest,
                                                      HttpProxyInterceptPipeline pipeline) throws Exception {
                                //匹配到百度首页跳转到淘宝
                                if (HttpUtil.checkUrl(pipeline.getHttpRequest(), "^lotsmg.dpm.org.cn/tyApi/zybprod/market/canBuyDays$")) {
                                    DefaultFullHttpResponse hookResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
                                    HttpVo vo = httpClientInvok.doGet(getSignStr());
                                    String content = vo.getContent();
                                    String reValue = "{\"status\":200,\"data\":\""+content+"\",\"statusDesc\":\"SUCCESS\",\"statusBizDesc\":\"SUCCESS\"}";
                                    hookResponse.content().writeBytes(reValue.getBytes());
                                    clientChannel.writeAndFlush(hookResponse);
                                    clientChannel.close();
                                    hookResponse.release();
                                }else {
                                    pipeline.beforeRequest(clientChannel, httpRequest);
                                }
                            }
                        });
                    }
                })
                .start(9999);
    }

    public  String getSignStr(){
        String param = "{\"days\":7,\"time\":\"%s %s\",\"timestamp\":%s}";
        return "http://27.155.83.114:3000/encodeDt?key="+ URLEncoder.encode(String.format(param, DateUtils.getNextDate(),DateUtils.getCurrentHourTime(),DateUtils.getNextTime()));
    }
}
